package com.ruoyi.platform.system.user.controller;

import com.ruoyi.base.annotaion.PermissionCheck;
import com.ruoyi.base.annotaion.TokenIgnore;
import com.ruoyi.base.controller.BaseController;
import com.ruoyi.base.pojo.HttpResult;
import com.ruoyi.base.utils.inputcheck.InputCheckUtils;
import com.ruoyi.platform.system.role.enums.FunctionEnum;
import com.ruoyi.platform.system.user.service.PasswordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-11-13 05:40:22
 */
@Api(tags = {"密码管理"})
@RestController
@Scope("request")
@RequestMapping("/password")
public class PasswordController extends BaseController {
    @Autowired
    private PasswordService passwordService;

    /**
     * 重置用户密码
     */
    @ApiOperation("重置用户密码")
    @PermissionCheck(FunctionEnum.EDIT_USER)
    @PatchMapping("/reset/{userId}")
    public HttpResult<String> resetPwd(@ApiParam(value = "用户id", required = true) @PathVariable long userId) {
        InputCheckUtils.checkLong(userId, "请选择用户");

        passwordService.resetPwd(userId);

        return success();
    }

    /**
     * 根据账号密码修改密码
     */
    @TokenIgnore
    @ApiOperation("根据账号密码修改密码")
    @PatchMapping("/account")
    public HttpResult<String> resetMyPwd(@ApiParam("账号") String account,
                                         @ApiParam(value = "旧密码", required = true) String old,
                                         @ApiParam(value = "新密码", required = true) String pwd) {
        InputCheckUtils.checkStr(account, "请输入账号");
        InputCheckUtils.checkStr(old, "请输入旧密码");
        InputCheckUtils.checkStr(pwd, "请输入新密码");

        passwordService.resetPwd(account, old, pwd);

        return success();
    }

    /**
     * 根据短信验证码修改密码
     */
    @TokenIgnore
    @ApiOperation("根据短信验证码修改密码")
    @PatchMapping("/phone/pwd")
    public HttpResult<String> resetPwdByCode(@ApiParam(value = "手机号", required = true) String phone,
                                             @ApiParam(value = "新密码", required = true) String pwd,
                                             @ApiParam(value = "短信验证码", required = true) String code) {
        InputCheckUtils.checkStr(phone, "请输入手机号");
        InputCheckUtils.checkStr(pwd, "请输入密码");
        InputCheckUtils.checkStr(code, "请输入短信验证码");

        passwordService.resetPwdByCode(phone, pwd, code);

        return success();
    }

    /**
     * 修改我的密码
     */
    @ApiOperation("修改我的密码")
    @PatchMapping("/mine")
    public HttpResult<String> resetMyPwd(@ApiParam(value = "新密码", required = true) String pwd) {
        InputCheckUtils.checkStr(pwd, "请输入新密码");

        passwordService.resetPwd(pwd);

        return success();
    }

    /**
     * 修改用户密码
     */
    @ApiOperation("修改用户密码")
    @PatchMapping("/{userId}")
    @PermissionCheck(FunctionEnum.EDIT_USER)
    public HttpResult<String> editUserPwd(@ApiParam(value = "用户id", required = true) @PathVariable long userId,
                                          @ApiParam(value = "新密码", required = true) String pwd) {
        InputCheckUtils.checkStr(pwd, "请输入新密码");

        passwordService.editUserPwd(userId, pwd);
        return success();
    }
}